/*
 * Copyright 2020-2021 redragon.dongbin
 *
 * This file is part of redragon-erp/赤龙ERP.

 * redragon-erp/赤龙ERP is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.

 * redragon-erp/赤龙ERP is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with redragon-erp/赤龙ERP.  If not, see <https://www.gnu.org/licenses/>.
 */
package com.erp.masterdata.calendar.controller;

import java.util.List;
import java.util.Map;
import javax.validation.Valid;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.redragon.framework.springmvc.ControllerSupport;
import org.apache.commons.lang.StringUtils;
import org.hibernate.exception.ConstraintViolationException;
import org.redragon.framework.hibernate.data.RDDataBox;
import org.redragon.framework.hibernate.model.Pages;
import org.redragon.framework.util.JsonDataUtil;
import org.redragon.framework.util.JsonUtil;
import com.erp.masterdata.calendar.dao.data.DataBox;
import com.erp.masterdata.calendar.dao.model.MdFinanceCalendar;
import com.erp.masterdata.calendar.dao.model.MdFinanceCalendarCO;
import com.erp.masterdata.calendar.service.MdFinanceCalendarPeriodService;
import com.erp.masterdata.calendar.service.MdFinanceCalendarService;

@Controller
@RequestMapping("/web/mdFinanceCalendar")
public class MdFinanceCalendarWebController extends ControllerSupport{
    
    //日志处理
    private Logger logger = LoggerFactory.getLogger(MdFinanceCalendarWebController.class);
    
    //服务层注入
    @Autowired
    private MdFinanceCalendarService mdFinanceCalendarService;
    @Autowired
    private MdFinanceCalendarPeriodService mdFinanceCalendarPeriodService;
    
    @Override
    public String getExceptionRedirectUrl() {
        return this.getFormatRedirectUrl("getMdFinanceCalendarList");
    }
    
    
    
    /**
     * 
     * @description 查询数据列表
     * @date 2021-01-21 19:54:36
     * @author 
     * @param pages
     * @param mdFinanceCalendarCO
     * @param model
     * @return String
     *
     */
    @RequestMapping("getMdFinanceCalendarList")
    public String getMdFinanceCalendarList(Pages pages, MdFinanceCalendarCO mdFinanceCalendarCO, Model model) {
        //分页查询数据
        if(pages.getPage()==0) {
            pages.setPage(1);
        }
        
        //分页查询数据
        List<MdFinanceCalendar> mdFinanceCalendarList = this.mdFinanceCalendarService.getDataObjects(pages, mdFinanceCalendarCO);
        
        //审批状态
        Map approveStatusMap = RDDataBox.getApproveStatusMap();
        //会计日历类型
        Map calendarTypeMap = DataBox.getCalendarType();
        
        //页面属性设置
        model.addAttribute("mdFinanceCalendarList", mdFinanceCalendarList);
        model.addAttribute("pages", pages);
        model.addAttribute("approveStatusMap", approveStatusMap);
        model.addAttribute("calendarTypeMap", calendarTypeMap);
        
        return "basic.jsp?content=mdFinanceCalendar/calendarList";
    }
    
    
    
    /**
     * 
     * @description 查询单条数据
     * @date 2021-01-21 19:54:36
     * @author 
     * @param mdFinanceCalendar
     * @param model
     * @return String
     *
     */
    @RequestMapping("getMdFinanceCalendar")
    public String getMdFinanceCalendar(MdFinanceCalendar mdFinanceCalendar, Model model) {
        //查询要编辑的数据
        if(mdFinanceCalendar!=null&&StringUtils.isNotBlank(mdFinanceCalendar.getCalendarCode())) {
            MdFinanceCalendar mdFinanceCalendarTemp = this.mdFinanceCalendarService.getDataObject(mdFinanceCalendar.getCalendarCode());
            if(mdFinanceCalendarTemp!=null) {
                mdFinanceCalendar = mdFinanceCalendarTemp;
            }
        }
        
        //审批状态
        Map approveStatusMap = RDDataBox.getApproveStatusMap();
        //会计日历类型
        Map calendarTypeMap = DataBox.getCalendarType();
        
        //页面属性设置
        model.addAttribute("mdFinanceCalendar", mdFinanceCalendar);
        model.addAttribute("calendarTypeMap", calendarTypeMap);
        model.addAttribute("approveStatusMap", approveStatusMap);
        
        return "basic.jsp?content=mdFinanceCalendar/calendarEdit";
    }
    
    
    
    /**
     * 
     * @description 编辑数据
     * @date 2021-01-21 19:54:36
     * @author 
     * @param mdFinanceCalendar
     * @param bindingResult
     * @param model
     * @return String
     *
     */
    @RequestMapping("editMdFinanceCalendar")
    public String editMdFinanceCalendar(@Valid MdFinanceCalendar mdFinanceCalendar, BindingResult bindingResult, Model model, RedirectAttributes attr) {
        //参数校验
        Map<String, String> errorMap = this.validateParameters(bindingResult, model);
        if(errorMap.size()>0) {
            return "forward:getMdFinanceCalendar";
        }
        
        try {
            //对当前编辑的对象初始化默认的字段
            
            //保存编辑的数据
            this.mdFinanceCalendarService.insertOrUpdateDataObject(mdFinanceCalendar);
            //提示信息
            attr.addFlashAttribute("hint", "success");
            attr.addAttribute("calendarCode", mdFinanceCalendar.getCalendarCode());
        }catch(Exception e){
            if(e.getCause().getClass()==ConstraintViolationException.class) {
                //提示信息
                attr.addFlashAttribute("hint", "hint");
                attr.addFlashAttribute("alertMessage", "会计日历编码已存在，请重新输入");
                return "redirect:getMdFinanceCalendarList";
            }else {
                throw e;
            }
        }
        
        return "redirect:getMdFinanceCalendar";
    }
    
    
    
    /**
     * 
     * @description 删除数据
     * @date 2021-01-21 19:54:36
     * @author 
     * @param mdFinanceCalendar
     * @return String
     *
     */
    @RequestMapping("deleteMdFinanceCalendar")
    public String deleteMdFinanceCalendar(MdFinanceCalendar mdFinanceCalendar, RedirectAttributes attr) {
        //删除数据前验证数据
        if(mdFinanceCalendar!=null&&mdFinanceCalendar.getCalendarId()!=null&&StringUtils.isNotBlank(mdFinanceCalendar.getCalendarCode())) {
            int periodCount = this.mdFinanceCalendarPeriodService.getPeriodCountByCalendarCode(mdFinanceCalendar.getCalendarCode());
            
            if(periodCount==0) {
                //删除数据
                this.mdFinanceCalendarService.deleteDataObject(mdFinanceCalendar);
                //提示信息
                attr.addFlashAttribute("hint", "success");
            }else {
                //提示信息
                attr.addFlashAttribute("hint", "hint");
                attr.addFlashAttribute("alertMessage", "会计日历已存在期间，无法删除");
            }
        }
        
        return "redirect:getMdFinanceCalendarList";
    }
    
    
    
    /**
     * 
     * @description 更新审批状态
     * @date 2020年8月4日
     * @author dongbin
     * @param code
     * @param approveStatus
     * @param attr
     * @return
     * @return String
     *
     */
    @RequestMapping("updateApproveStatus")
    public String updateApproveStatus(String code, String approveStatus, RedirectAttributes attr) {
        
        if(StringUtils.isNotBlank(code)&&StringUtils.isNotBlank(approveStatus)) {
            //更新审核状态
            this.mdFinanceCalendarService.updateApproveStatus(code, approveStatus);
            //提示信息
            attr.addFlashAttribute("hint", "success");
            attr.addAttribute("calendarCode", code);
        }else {
            //提示信息
            attr.addFlashAttribute("hint", "hint");
            attr.addFlashAttribute("alertMessage", "提交或审批数据错误");
            attr.addAttribute("calendarCode", code);
        }
        
        return "redirect:getMdFinanceCalendar";
    }
}
